package com.focus.wecenter.web.controller;

import com.focus.wecenter.pojo.User;
import com.focus.wecenter.service.UserService;
import com.focus.wecenter.web.vo.UserForm;
import com.focus.wecenter.util.Message;
import com.focus.wecenter.web.controller.common.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * 用户控制器
 *
 * @author Lv Jing
 * @date 2018年01月04日 21:04
 */
@Controller
@RequestMapping("user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String showLogin() {
        return "user/login";
    }

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String showRegister() {
        return "user/register";
    }

    @RequestMapping(value = "/ajax/register", method = RequestMethod.POST)
    @ResponseBody
    public Message doRegister(UserForm user, HttpSession session) {
        System.out.println(user);
        Message message = new Message();

        // 检查邮箱是否重复
        if (userService.checkEmail(user.getEmail())) {
            message.setSuccess(false);
            message.setData("邮箱已经被使用");
            return message;
        }

        // 检查验证码是否正确
        Object captcha = session.getAttribute("captcha_session");
        if (!user.getSeccode_verify().equalsIgnoreCase((String) captcha)) {
            message.setSuccess(false);
            message.setData("请填写正确的验证码");
            return message;
        }

        message.setSuccess(true);
        message.setData("index.html");
        userService.register(user);
        return message;
    }

    @RequestMapping(value = "/ajax/check_username", method = RequestMethod.POST)
    @ResponseBody
    public Message checkUsername(String username) {
        Message message = new Message();
        if (userService.checkUsername(username)) {
            message.setSuccess(false);
            message.setData("用户名已被注册");
        } else {
            message.setSuccess(true);
        }
        return message;
    }

    @RequestMapping(value = "/ajax/login", method = RequestMethod.POST)
    @ResponseBody
    public Message doLogin(UserForm userForm, HttpSession session) {
        Message message = new Message();
        User user = userService.login(userForm.getUser_name(), userForm.getPassword());
        if (user == null) {
            message.setSuccess(false);
            message.setData("请输入正确的帐号或密码");
            return message;
        }

        // TODO:这里可以考虑把记住我功能实现，用cookie
        super.setSessionUser(session, user);
        message.setSuccess(true);
        message.setData("index.html");
        return message;
    }

    /**
     * 显示用户基本资料
     */
    @RequestMapping("/center/profile")
    public String showUserProfile(HttpSession session, Model model) {
        User user = userService.findByID(this.getSessionUserID(session));
        model.addAttribute("profile", user);
        return "user/profile";
    }

    /**
     * 用户退出
     */
    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        super.removeSessionUser(session);
        return "redirect:/index";
    }
}
